package com.imufe.chemicalenterprisedangerwarning.util;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author BaiBan
 * @since 2022/9/20
 */

public class Generator {

    public final static String url = "jdbc:mysql://localhost:3306/chemical_enterprise_danger_warning?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8";
    public final static String username = "root";
    public final static String password = "root";

    public static void main(String[] args) {

        // 新建路径
        String dirPath = System.getProperty("user.dir") + "/src/main/java";
        String xmlPath = System.getProperty("user.dir") + "/src/main/resources/mappers";

        // 替换路径
//        String dirPath = System.getProperty("user.dir") + "/java";
//        String xmlPath = System.getProperty("user.dir") + "/mappers";

        FastAutoGenerator.create(url, username, password)
                // 全局配置
                .globalConfig(builder -> {
                    builder.author("BaiBan") // 设置作者
                            .enableSwagger() // 开启 swagger模式
                            .outputDir(dirPath); // 指定输出目录
                })

                // 包配置
                .packageConfig(builder -> builder.parent("com.imufe.chemicalenterprisedangerwarning") // 设置父包名
                        .pathInfo(Collections.singletonMap(OutputFile.xml, xmlPath)) // 设置mapperXml生成路径
                        .entity("entity.po"))


                // 策略配置
                .strategyConfig((scanner, builder) ->
                        builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入 all")))
                            // controller配置
                            .controllerBuilder()
                                .enableRestStyle()
                                .enableHyphenStyle()

                            // service配置
                            .serviceBuilder()
                                .formatServiceFileName("%sService")
                            // mapper配置
                            .mapperBuilder()
                                .enableBaseResultMap()
                                .formatMapperFileName("%sMapper")

                            // entity配置
                            .entityBuilder()
                                .enableLombok()
                                .enableRemoveIsPrefix()
                                .logicDeleteColumnName("is_deleted")
                                .addTableFills(
                                        new Column("create_time", FieldFill.INSERT),
                                        new Column("update_time", FieldFill.INSERT_UPDATE)
                                )
                                .formatFileName("%sPO")
                            .build())
                .execute();
    }

    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
